package bg.btanev.eightyfour.client.view;

import bg.btanev.eightyfour.client.RootAppControler.RootTabDisplay;
import bg.btanev.eightyfour.client.composite.BasicTabComposite;

import com.google.gwt.event.logical.shared.HasBeforeSelectionHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author B.Tanev
 *
 */
public class TabContainerView extends Composite implements RootTabDisplay {

	private final DecoratedTabPanel tabContainerPanel;

	public TabContainerView() {

		tabContainerPanel = new DecoratedTabPanel();
		tabContainerPanel.setAnimationEnabled(true);
		initWidget(tabContainerPanel);
		tabContainerPanel.setWidth("100%");
	}

	@Override
	public void addTab(BasicTabComposite tab) {
		if (tab == null) {
			return;
		}

		tabContainerPanel.add(tab, tab.getTabCaption(), false);
		if (tabContainerPanel.getWidgetCount() == 1) {
			tabContainerPanel.selectTab(0);
		}
	}

	@Override
	public HasSelectionHandlers<Integer> getSelectionHandlers() {
		return tabContainerPanel;
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasBeforeSelectionHandlers<Integer> getBeforeSelectionHandlers() {
		return tabContainerPanel;
	}

	@Override
	public BasicTabComposite getTab(int pos) {
		BasicTabComposite tabComposite = null;

		if (pos >= 0) {
			Widget tabWidget = tabContainerPanel.getWidget(pos);
			if (tabWidget instanceof BasicTabComposite) {
				tabComposite = (BasicTabComposite) tabWidget;

			}
		}

		return tabComposite;
	}
}
